package com.ophiux.wechat.utils;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Map生成器工具类
 * @author chenh
 *
 */
public class MapBuilder {

	private static MapBuilder  mapBuilder;
	
	public static MapBuilder getInstance(){
		if(mapBuilder == null){
			mapBuilder = new MapBuilder();
		}
		return mapBuilder;
	}
	
	 /**
	  * 生成SortMap
	  * @param keys  键集合
	  * @param values 值集合 和检一一对应
	  * @return
	  */
	public static SortedMap buildSortMap(String[] keys,String[] values){
		
		 SortedMap<String, String> sortMap = new TreeMap<String, String>();
		 
		 /**
		  * 1.判空操作
		  * 2.判断键和值是否长度对应
		  */
		 if(keys==null||keys.length==0
				 ||values==null||values.length==0
				 || keys.length != values.length){
					 
			 sortMap = null;
			 return sortMap;
		 }
		 
		 /**
		  * 循环组装Map
		  */
		 for(int index=0;index<keys.length;index++){
			 if(keys[index]==null){
				 continue;
			 }
			 sortMap.put(keys[index], values[index]);
		 }
		 
		 return sortMap;
	}
	
	
	public static void main(String[] args) {
		String [] keys = {"appid","mch_id","nonce_str","body","out_trade_no",
				"total_fee","spbill_create_ip","notify_url","trade_type","openid"};
		String []values = {"wx35a390b81ab01ba6","1379683702","1500516019",
				"预约挂号/门诊支付","9001201612051458042",
				"3.0","175.155.112.40",
				"http://wx.sxsey.cn:8918/ophiuxAppointDataService/weixingzh/doCallBack.do",
				"JSAPI","oMx83w5WDRYHk9l1xv556GIVt9Ww"};
		
		SortedMap<String, String> packageParams = MapBuilder.getInstance()
									.buildSortMap(keys, values);
		SortedMap<String,String> map = 
				MapBuilder.getInstance().buildSortMap(keys,values);
		
//		 Set<Entry<String,String>> entry1=map.entrySet();  
//		  Iterator<Entry<String,String>> it=entry1.iterator();  
//		  while(it.hasNext())  
//		  {  
//		   Entry<String,String> entry=it.next();  
//		   System.out.println("排序之后:"+entry.getKey()+" 值"+entry.getValue());  
//		  }  
		
		  String api_key = "sxsdermyy120sxsdermyy120sxsdermy";
		  String sign = WeChatSignUtil.getInstance(api_key)
				  .createSign(packageParams);
	      System.out.println(sign);
		  
	      
	      
	      
	}
	
	
}
